package com.mmall.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class ThreadPoolExample8 {

    public static void main(String[] args) throws Exception{
        ExecutorService service = Executors.newCachedThreadPool();

        FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("正在计算结果...");
                Thread.sleep(3000);
                return 1;
            }
        });
        // 执行任务
        service.submit(task);
        // 立刻执行下一行代码
        System.out.println("计算仍然在进行，我们做点别的...");
        // 执行一段时间后，回去取计算结果
        Integer result = task.get();
        System.out.println("拿到的结果为：" + result);
        service.shutdown();
    }
}
